package com.zp.kidshop.common.exception;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;

import com.zp.kidshop.common.pojo.SysProp;
import com.zp.kidshop.common.util.Ascall;
import com.zp.kidshop.common.util.LogUtil;

/**
 * 
 * @Title: ExceptionMapperSupport.java
 * @Package com.minghang.common.exception.aop 
 * @Description: 统一异常处理类
 * @author 张鹏  
 * @date 2017年2月16日 下午2:58:37 
 * @version V1.0
 */
@Controller
public class ExceptionMapperSupport implements HandlerExceptionResolver {
	private static Pattern pattern = Pattern.compile("\r\n");

	//此处拦截异常
	public ModelAndView resolveException(HttpServletRequest request,
			HttpServletResponse response, Object handler, Exception ex) {
	    ModelAndView mv = new ModelAndView();
//	    
//		Map<String, Object> model = new HashMap<String, Object>();
//        
//		String errStr = null;
//		//此次不使用，没有要求说，需要把get和post请求分开拋错
////			if (!(request.getHeader("accept").indexOf("application/json") > -1 || (request  
////					.getHeader("X-Requested-With")!= null && request  
////					.getHeader("X-Requested-With").indexOf("XMLHttpRequest") > -1))) {
////				
////			}else{}
//		if (ex instanceof BaseUnException) {
//			BaseUnException ue = (BaseUnException)ex;            // 转换
//
//			if (null != (errStr = ue.getErrCode())) {
//				LogUtil.addLog("[Error] Code:".concat(errStr)
//					   .concat(",Msg:").concat(ex.getMessage()));// 写log
//				LogUtil.print(LogUtil.WARN, ex.getCause());      // 输出log
//			}
//			ue = null;
//		}else if (ex instanceof BaseNeException){                // 与上面处理方式相同
//			BaseNeException ne = (BaseNeException)ex;
//
//			if (null != (errStr = ne.getErrCode())) {
//				LogUtil.addLog("[Error] Code:".concat(errStr)
//						   .concat(",Msg:").concat(ex.getMessage()));
//					LogUtil.print(LogUtil.WARN, ex.getCause());
//			}
//			ne = null;
//		}else {                      // 其他异常
//			if(null != ex.getCause()){
//				LogUtil.addLog("[Error] Code:".concat(errStr)
//						    .concat(",Msg:").concat(ex.getMessage()));
//					LogUtil.print(LogUtil.WARN, ex.getCause());
//			}
//		}
//		if(ex != null){
//			if (!SysProp.isDebug()) {
//				if(ex.getMessage() != null){
//					String[] erroMsg = ex.getMessage().split(",");
//					//一小时获取才能获取数据的长度是3，
//					//故：判断非3的异常发送邮件
//					if(erroMsg.length != 3){
////						sendErrorMail(ex);
//					}
//				}
//			}
//		}
//		
//		model.put("err_code", errStr==null?"JEC00":errStr);
//		//涉及到转向问题，中文内容，加密。
//		model.put("err_msg", ex.getMessage()==null?"":Ascall.encoder(ex.getMessage()));
//		
//		// 构建返回对象  --重定向
//        mv.setViewName("redirect:../public/erro"); 
//        mv.addAllObjects(model);
        return mv;
	}

	/**
	 * 发送邮件
	 **/
	private void sendErrorMail(Throwable e){
		LogUtil.setLogList(new LinkedList<String>());
		LogUtil.addLog(LogUtil.getExceptionInfo(e));// 获取异常
		String mailContent = LogUtil.buildLogBody();// 整理异常
		LogUtil.printC(LogUtil.FATAL, mailContent); // 输出异常
		Matcher matcher = pattern.matcher(mailContent);
//		executorService.sendMultipleMail("WEB端2.0接口调用异常", matcher.replaceAll("<br/>"),null);// 发送异常
	}
}